{% embed 'Embed/table_embed.html.twig' %}
    {% import 'Import/app_import.html.twig' as appImport %}

    {% set columns = {'uuid': true, 'name': true, 'nodes': true, 'creation_date': true, '_buttons': true} %}

    {% set is_granted_nodes = is_granted('NODES_LIST', 'node') %}

    {% set is_granted_dangling_indices_import = is_granted('DANGLING_INDICES_IMPORT', 'global') %}

    {% set is_granted_dangling_indices_delete = is_granted('DANGLING_INDICES_DELETE', 'global') %}

    {% set modalReference = 0 %}

    {% block thead %}
        <tr>
            {% for column, display in columns %}
                {% if true == display %}
                    {% if 'uuid' == column %}
                        <th>
                            {{ 'uuid'|trans }}
                        </th>
                    {% endif %}

                    {% if 'name' == column %}
                        <th>
                            {{ 'name'|trans }}
                        </th>
                    {% endif %}

                    {% if 'nodes' == column %}
                        <th>
                            {{ 'nodes'|trans }}
                        </th>
                    {% endif %}

                    {% if 'creation_date' == column %}
                        <th>
                            {{ 'creation_date'|trans }}{{ appImport.badge({'title': 'sort_desc'|trans, 'context': 'secondary'}) }}
                        </th>
                    {% endif %}

                    {% if '_buttons' == column %}
                        <th>
                            &nbsp;
                        </th>
                    {% endif %}
                {% endif %}
            {% endfor %}
        </tr>
    {% endblock %}

    {% block tbody %}
        {% for row in indices.rows %}
            <tr>
                {% for column, display in columns %}
                    {% if true == display %}
                        {% if 'uuid' == column %}
                            <td>
                                {{ row['index_uuid'] }}
                            </td>
                        {% endif %}

                        {% if 'name' == column %}
                            <td>
                                {{ row['index_name'] }}
                            </td>
                        {% endif %}

                        {% if 'nodes' == column %}
                            <td>
                                {% for node_id in row['node_ids'] %}
                                    {% if nodes[node_id] is defined %}
                                        {% if is_granted_nodes %}
                                            <a class="{{ theme_link_secondary }}" href="{{ path('nodes_read', {'node': nodes[node_id]}) }}">{{ nodes[node_id] }}</a>{% if false == loop.last %}, {% endif %}
                                        {% else %}
                                            {{ nodes[node_id] }}{% if false == loop.last %}, {% endif %}
                                        {% endif %}
                                    {% else %}
                                        {{ node_id }}{% if false == loop.last %}, {% endif %}
                                    {% endif %}
                                {% endfor %}
                            </td>
                        {% endif %}

                        {% if 'creation_date' == column %}
                            <td>
                                {{ row['creation_date_millis']|human_datetime }}
                            </td>
                        {% endif %}

                        {% if '_buttons' == column %}
                            <td>
                                {% if is_granted_dangling_indices_import %}
                                    <button type="button" class="btn btn-primary btn-sm" data-bs-toggle="modal" data-bs-target="#modal{{ modalReference }}">
                                        {{ 'dangling_indices_import'|trans }}
                                    </button>
                                    <div class="modal fade" id="modal{{ modalReference }}" tabindex="-1" role="dialog" aria-labelledby="modalLabel{{ modalReference }}" aria-hidden="true">
                                        <div class="modal-dialog" role="document">
                                            <div class="modal-content {{ theme_modal_content }}">
                                                <div class="modal-header">
                                                    <h5 class="modal-title" id="modalLabel{{ modalReference }}">{{ 'dangling_indices_import'|trans }}</h5>
                                                    <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="{{ 'cancel'|trans }}"></button>
                                                </div>
                                                <div class="modal-body text-left">
                                                    <p>
                                                        <strong>{{ 'uuid'|trans }}</strong><br>
                                                        {{ row['index_uuid'] }}
                                                    </p>
                                                    <p>
                                                        <strong>{{ 'name'|trans }}</strong><br>
                                                        {{ row['index_name'] }}
                                                    </p>
                                                </div>
                                                <div class="modal-footer">
                                                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">{{ 'cancel'|trans }}</button>
                                                    <a class="{{ theme_link_primary }}" href="{{ path('dangling_indices_import', {'index': row['index_uuid']}) }}" class="btn btn-primary">{{ 'confirm'|trans }}</a>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    {% set modalReference = modalReference + 1 %}
                                {% endif %}

                                {% if is_granted_dangling_indices_delete %}
                                    <button type="button" class="btn btn-primary btn-sm" data-bs-toggle="modal" data-bs-target="#modal{{ modalReference }}">
                                        {{ 'dangling_indices_delete'|trans }}
                                    </button>
                                    <div class="modal fade" id="modal{{ modalReference }}" tabindex="-1" role="dialog" aria-labelledby="modalLabel{{ modalReference }}" aria-hidden="true">
                                        <div class="modal-dialog" role="document">
                                            <div class="modal-content {{ theme_modal_content }}">
                                                <div class="modal-header">
                                                    <h5 class="modal-title" id="modalLabel{{ modalReference }}">{{ 'dangling_indices_delete'|trans }}</h5>
                                                    <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="{{ 'cancel'|trans }}"></button>
                                                </div>
                                                <div class="modal-body text-left">
                                                    <p>
                                                        <strong>{{ 'uuid'|trans }}</strong><br>
                                                        {{ row['index_uuid'] }}
                                                    </p>
                                                    <p>
                                                        <strong>{{ 'name'|trans }}</strong><br>
                                                        {{ row['index_name'] }}
                                                    </p>
                                                </div>
                                                <div class="modal-footer">
                                                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">{{ 'cancel'|trans }}</button>
                                                    <a class="{{ theme_link_primary }}" href="{{ path('dangling_indices_delete', {'index': row['index_uuid']}) }}" class="btn btn-primary">{{ 'confirm'|trans }}</a>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    {% set modalReference = modalReference + 1 %}
                                {% endif %}
                            </td>
                        {% endif %}
                    {% endif %}
                {% endfor %}
            </tr>
        {% endfor %}
    {% endblock %}
{% endembed %}
